using System;
using System.Collections.Generic;
using Infragistics.UltraChart.Resources.Appearance;
using PluginFrame;

namespace PcrDataStructure
{
    [Serializable]
    public class HRMExperiment : PcrExperiment, IHRMExperiment, IPcrExperiment
    {
        [NonSerialized]
        private int mGroupCount;

        [NonSerialized]
        private bool mHasUndeterminedCurve;

        private string[] mHRMGroupString;

        public string[] HRMGroupString
        {
            get
            {
                return mHRMGroupString;
            }
            set
            {
                mHRMGroupString = value;
            }
        }

        public bool HasUndeterminedCurve
        {
            get
            {
                return mHasUndeterminedCurve;
            }
            set
            {
                mHasUndeterminedCurve = value;
            }
        }

        public int GroupCount
        {
            get
            {
                return mGroupCount;
            }
            set
            {
                mGroupCount = value;
            }
        }

        public HRMExperiment(IPcrStructure pcrStructure)
            : base(pcrStructure)
        {
            mSystemType = PcrSystemType.GeneScan;
            mCurveColorStyle = CurveColorStyle.Result;
            InitializeHRMGroupString();
        }

        public XYSeries GetHRMBeforeMeltingStartCurves(IPcrExperimentProject project)
        {
            try
            {
                if (mHRMBeforeMeltingStartCurves == null)
                {
                    return null;
                }
                if (!mHRMBeforeMeltingStartCurves.ContainsKey(project))
                {
                    return null;
                }
                return mHRMBeforeMeltingStartCurves[project];
            }
            catch (Exception)
            {
                return null;
            }
        }

        public XYSeries GetHRMBeforeMeltingStopCurves(IPcrExperimentProject project)
        {
            try
            {
                if (mHRMBeforeMeltingStopCurves == null)
                {
                    return null;
                }
                if (!mHRMBeforeMeltingStopCurves.ContainsKey(project))
                {
                    return null;
                }
                return mHRMBeforeMeltingStopCurves[project];
            }
            catch (Exception)
            {
                return null;
            }
        }

        public XYSeries GetHRMAfterMeltingStopCurves(IPcrExperimentProject project)
        {
            try
            {
                if (mHRMAfterMeltingStopCurves == null)
                {
                    return null;
                }
                if (!mHRMAfterMeltingStopCurves.ContainsKey(project))
                {
                    return null;
                }
                return mHRMAfterMeltingStopCurves[project];
            }
            catch (Exception)
            {
                return null;
            }
        }

        public XYSeries GetHRMAfterMeltingStartCurves(IPcrExperimentProject project)
        {
            try
            {
                if (mHRMAfterMeltingStartCurves == null)
                {
                    return null;
                }
                if (!mHRMAfterMeltingStartCurves.ContainsKey(project))
                {
                    return null;
                }
                return mHRMAfterMeltingStartCurves[project];
            }
            catch (Exception)
            {
                return null;
            }
        }

        public void InitializeHRMGroupString()
        {
            mHRMGroupString = new string[7];
            mHRMGroupString[0] = mResourceManager.GetString("UndeterminedString");
            for (int i = 1; i < 7; i++)
            {
                mHRMGroupString[i] = mResourceManager.GetString("HRMResultString") + i;
            }
        }

        protected override bool CheckColumnInExperiment(IColumn column)
        {
            if (column == null)
            {
                return false;
            }
            if (column.Classification == ColumnClassification.GeneScanResult)
            {
                return true;
            }
            if (column.Classification == ColumnClassification.RelativeQuantificationResult || column.Classification == ColumnClassification.GeneTypeResult || column.Classification == ColumnClassification.AnalysisResult || column.Classification == ColumnClassification.CalculationResult || column.Classification == ColumnClassification.MeltingCurveResult || column.Classification == ColumnClassification.AlleleInformation)
            {
                return false;
            }
            if (column.Classification == ColumnClassification.AlleleResult)
            {
                if (column.Name.Equals("QualityKey"))
                {
                    column.DisplayName = "Score";
                }
                else if (column.Name.Equals("Rn1Key") || column.Name.Equals("Rn2Key"))
                {
                    return false;
                }
                return true;
            }
            return true;
        }

        public override void CheckNewColumnExist()
        {
        }

        protected override List<IPcrExperimentProject> CheckExperimentImportStandardCurve()
        {
            return null;
        }

        protected override bool GetProjectMeltingStatus(IPcrExperimentProject project, out IMeltingSegment meltingSegment)
        {
            meltingSegment = project.PcrProgram.GetHRMSegment(0);
            if (meltingSegment != null)
            {
                return meltingSegment.IsAvailable;
            }
            return false;
        }

        protected override void InitializeProjectItemOption(ref IAbsoluteAnalysisParameterConfiguration analysisOption, ref IMeltingAnalysisParameterConfiguration meltingOption, IProjectItem projectItem, IPcrExperimentProject project, int tubeIndex)
        {
            analysisOption = projectItem.TubeAnalysisParameterConfiguration[tubeIndex] as IAbsoluteAnalysisParameterConfiguration;
            analysisOption.Project = project;
            (analysisOption as AnalysisParameterConfiguration).IsNeedReAnalysis = true;
            (analysisOption as AnalysisParameterConfiguration).ProjectChannel = projectItem.ChannelConfiguration.Position;
            meltingOption = null;
        }

        protected override void CalculateSpecialData(string analyzeMethod, IPcrExperimentProject project, int projectIndex)
        {
            if (mCurveColorStyle != CurveColorStyle.Result && mCurveColorStyle != CurveColorStyle.Type)
            {
                mCurveColorStyle = CurveColorStyle.Result;
            }
            if (analyzeMethod == "Analyze")
            {
                mAnalysisService.AnalyzeHRMProject(project, projectIndex, this);
            }
            else
            {
                mHasUndeterminedCurve = false;
            }
        }

        protected override void InitializeMeltingVariables()
        {
            mHRMBeforeMeltingStartCurves = new Dictionary<IPcrExperimentProject, XYSeries>();
            mHRMBeforeMeltingStopCurves = new Dictionary<IPcrExperimentProject, XYSeries>();
            mHRMAfterMeltingStartCurves = new Dictionary<IPcrExperimentProject, XYSeries>();
            mHRMAfterMeltingStopCurves = new Dictionary<IPcrExperimentProject, XYSeries>();
        }

        protected override void ClearResultInformation(IPcrCell pcrCell, int channelIndex)
        {
            pcrCell.SetCellInformation("MethodKey", channelIndex, string.Empty);
            pcrCell.SetCellInformation("QualityKey", channelIndex, string.Empty);
            pcrCell.SetCellInformation("CallKey", channelIndex, string.Empty);
            pcrCell.SetCellInformation("InterpretationKey", channelIndex, string.Empty);
            pcrCell.SetCellInformation("MeasurementResultKey", channelIndex, string.Empty);
        }

        public void DisplayHRMBeforeMeltingStartCurves(IPcrExperimentProject project, bool isDisplay, bool setCurvesInVisible)
        {
            try
            {
                if (mHRMBeforeMeltingStartCurves == null || !mHRMBeforeMeltingStartCurves.ContainsKey(project))
                {
                    return;
                }
                XYSeries xYSeries = mHRMBeforeMeltingStartCurves[project];
                if (xYSeries != null)
                {
                    if (setCurvesInVisible)
                    {
                        xYSeries.Visible = false;
                    }
                    if (xYSeries.Visible != isDisplay)
                    {
                        xYSeries.Visible = isDisplay;
                    }
                }
            }
            catch (Exception)
            {
            }
        }

        public void DisplayHRMBeforeMeltingStopCurves(IPcrExperimentProject project, bool isDisplay, bool setCurvesInVisible)
        {
            try
            {
                if (mHRMBeforeMeltingStopCurves == null || !mHRMBeforeMeltingStopCurves.ContainsKey(project))
                {
                    return;
                }
                XYSeries xYSeries = mHRMBeforeMeltingStopCurves[project];
                if (xYSeries != null)
                {
                    if (setCurvesInVisible)
                    {
                        xYSeries.Visible = false;
                    }
                    if (xYSeries.Visible != isDisplay)
                    {
                        xYSeries.Visible = isDisplay;
                    }
                }
            }
            catch (Exception)
            {
            }
        }

        public void DisplayHRMAfterMeltingStartCurves(IPcrExperimentProject project, bool isDisplay, bool setCurvesInVisible)
        {
            try
            {
                if (mHRMAfterMeltingStartCurves == null || !mHRMAfterMeltingStartCurves.ContainsKey(project))
                {
                    return;
                }
                XYSeries xYSeries = mHRMAfterMeltingStartCurves[project];
                if (xYSeries != null)
                {
                    if (setCurvesInVisible)
                    {
                        xYSeries.Visible = false;
                    }
                    if (xYSeries.Visible != isDisplay)
                    {
                        xYSeries.Visible = isDisplay;
                    }
                }
            }
            catch (Exception)
            {
            }
        }

        public void DisplayHRMAfterMeltingStopCurves(IPcrExperimentProject project, bool isDisplay, bool setCurvesInVisible)
        {
            try
            {
                if (mHRMAfterMeltingStopCurves == null || !mHRMAfterMeltingStopCurves.ContainsKey(project))
                {
                    return;
                }
                XYSeries xYSeries = mHRMAfterMeltingStopCurves[project];
                if (xYSeries != null)
                {
                    if (setCurvesInVisible)
                    {
                        xYSeries.Visible = false;
                    }
                    if (xYSeries.Visible != isDisplay)
                    {
                        xYSeries.Visible = isDisplay;
                    }
                }
            }
            catch (Exception)
            {
            }
        }

        public void RefreshHRMBeforeMeltingStartCurves(double value, IPcrExperimentProject project, bool isDisplay, bool setCurvesInvisible)
        {
            XYSeries hRMBeforeMeltingStartCurves = GetHRMBeforeMeltingStartCurves(project);
            if (hRMBeforeMeltingStartCurves == null)
            {
                return;
            }
            foreach (XYDataPoint point in hRMBeforeMeltingStartCurves.Points)
            {
                point.ValueX = value;
            }
            DisplayHRMBeforeMeltingStartCurves(project, isDisplay, setCurvesInvisible);
        }

        public void RefreshHRMBeforeMeltingStopCurves(double value, IPcrExperimentProject project, bool isDisplay, bool setCurvesInvisible)
        {
            XYSeries hRMBeforeMeltingStopCurves = GetHRMBeforeMeltingStopCurves(project);
            if (hRMBeforeMeltingStopCurves == null)
            {
                return;
            }
            foreach (XYDataPoint point in hRMBeforeMeltingStopCurves.Points)
            {
                point.ValueX = value;
            }
            DisplayHRMBeforeMeltingStopCurves(project, isDisplay, setCurvesInvisible);
        }

        public void RefreshHRMAfterMeltingStartCurves(double value, IPcrExperimentProject project, bool isDisplay, bool setCurvesInvisible)
        {
            XYSeries hRMAfterMeltingStartCurves = GetHRMAfterMeltingStartCurves(project);
            if (hRMAfterMeltingStartCurves == null)
            {
                return;
            }
            foreach (XYDataPoint point in hRMAfterMeltingStartCurves.Points)
            {
                point.ValueX = value;
            }
            DisplayHRMAfterMeltingStartCurves(project, isDisplay, setCurvesInvisible);
        }

        public void RefreshHRMAfterMeltingStopCurves(double value, IPcrExperimentProject project, bool isDisplay, bool setCurvesInvisible)
        {
            XYSeries hRMAfterMeltingStopCurves = GetHRMAfterMeltingStopCurves(project);
            if (hRMAfterMeltingStopCurves == null)
            {
                return;
            }
            foreach (XYDataPoint point in hRMAfterMeltingStopCurves.Points)
            {
                point.ValueX = value;
            }
            DisplayHRMAfterMeltingStopCurves(project, isDisplay, setCurvesInvisible);
        }

        protected override bool CheckStandardCell(IPcrCell pcrCell, int channelIndex)
        {
            return true;
        }
    }
}
